package com.fametal.ui.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.fametal.GlobalConstant.Settings;
import com.fametal.R;
import com.fametal.ui.adapter.PhoneNumAdapter;
import com.fametal.ui.adapter.PhoneNumAdapter.RemoveListener;
import com.fametal.ui.model.Phone;
import com.fametal.ui.utils.DialogUtils;
import com.fametal.ui.utils.Utils;
import com.fametal.ui.utils.Utils.GetFinishedListener;

public class TELControlActivity extends Activity implements GetFinishedListener {
	
	private static final String GET_BOOK = Settings.IP + "cgi-bin/phone_control.cgi?call=B";
	private ListView phoneList;
	private EditText input_phonenum;
	private Dialog mDialog;
	private List<Phone> phoneDate;
	private PhoneNumAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tel_control);
		input_phonenum = (EditText) findViewById(R.id.input_phonenum);
		phoneList = (ListView) findViewById(R.id.phonelist);
		phoneDate = new ArrayList<Phone>();
		adapter = new PhoneNumAdapter(this, phoneDate);
		adapter.setRemoveListener(listener);
		phoneList.setAdapter(adapter);
	}
	
	private RemoveListener listener = new RemoveListener() {
		@Override
		public void remove(Phone phone) {
			String url = Settings.IP + "cgi-bin/phone_control.cgi?call=D&name="+phone.name+"&num="+phone.number;
			Utils.get(TELControlActivity.this, url, false);
		}
	};
	
	protected void onResume() {
		super.onResume();
		Utils.setGetFinishedListener(this);
		Utils.get(this, GET_BOOK, true);
	};
	
	@Override
	public void finished(String result) {
		try {
//			result = "{\"phones\":[{\"name\":\"tom1\",\"phone\":\"8001\"}, {\"name\":\"tom2\",\"phone\":\"8002\"}, {\"name\":\"tom3\",\"phone\":\"8003\"}, {\"name\":\"tom4\",\"phone\":\"8004\"}, {\"name\":\"tom5\",\"phone\":\"8005\"}]}";
			JSONObject jsonObject = new JSONObject(result);
			JSONArray phones = jsonObject.getJSONArray("phones");
			Log.d("david", "phones length = " + phones.length());
			for(int i = 0; i < phones.length(); i++){
				JSONObject phone = phones.getJSONObject(i);
				phoneDate.add(new Phone(phone.getString("name"),phone.getString("phone")));
				adapter.notifyDataSetChanged();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	StringBuilder num = new StringBuilder();
	private int total;
	private boolean hangup = true;
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tel_offhook:
			Button bt = (Button)view;
			if(hangup == true){
				final String phoneNum = input_phonenum.getText().toString();
				if(TextUtils.isEmpty(phoneNum)) {
					DialogUtils.alert(this, "提示", "请先输入电话号码？", null);
					return;
				}
				bt.setText(getResources().getString(R.string.hanghook));
				bt.setBackgroundResource(R.drawable.it_btn_num_red_2);
				hangup = false;
				String url = Settings.IP + "/cgi-bin/phone_control.cgi?call=Z" + phoneNum;
				Utils.get(TELControlActivity.this, url, false);
			}else{
				bt.setText(getResources().getString(R.string.offhook));
				bt.setBackgroundResource(R.drawable.ir_btn_num_green_1);
				hangup = true;
				String url = Settings.IP + "/cgi-bin/phone_control.cgi?call=G";
				Utils.get(TELControlActivity.this, url, false);
			}
			break;
		case R.id.tel_one:
			num.append(1);
			total++;
			break;
		case R.id.tel_two:
			num.append(2);
			total++;
			break;
		case R.id.tel_three:
			total++;
			num.append(3);
			break;
		case R.id.tel_backspace:
			if(total <= 0)
				return;
			num.deleteCharAt(total - 1);
			total--;
			break;
		case R.id.tel_four:
			num.append(4);
			total++;
			break;
		case R.id.tel_five:
			num.append(5);
			total++;
			break;
		case R.id.tel_six:
			num.append(6);
			total++;
			break;
		case R.id.tel_add:
			final String phoneNum = input_phonenum.getText().toString();
			if(TextUtils.isEmpty(phoneNum)) {
				DialogUtils.alert(this, "提示", "请先输入电话号码？", null);
				return;
			}
			mDialog = DialogUtils.makeConfirmDialogEditText(this, "是否保存电话号码？", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					EditText text = (EditText) mDialog.findViewById(1222);
					String name = text.getText().toString();
					if(TextUtils.isEmpty(name)){
						DialogUtils.alert(TELControlActivity.this, "提示", "请输入姓名？", null);
					}
					Phone phone = new Phone(name, phoneNum);
					phoneDate.add(phone);
					adapter.notifyDataSetChanged();
					String url = Settings.IP + "cgi-bin/phone_control.cgi?call=A&name="+name+"&num="+phoneNum;
					Utils.get(TELControlActivity.this, url, false);
				}
			}, null);
			mDialog.show();
			break;
		case R.id.tel_seven:
			num.append(7);
			total++;
			break;
		case R.id.tel_eight:
			num.append(8);
			total++;
			break;
		case R.id.tel_nine:
			num.append(9);
			total++;
			break;
		case R.id.tel_clear:
			if(total == 0)
				return;
			num.delete(0, total);
			total = 0;
			break;
		case R.id.tel_asterisk:
			num.append("*");
			total++;
			break;	
		case R.id.tel_zero:
			num.append(0);
			total++;
			break;
		case R.id.tel_wellnumber:
			num.append("#");
			total++;
			break;
		case R.id.tel_recall:
			String telNum = input_phonenum.getText().toString();
			if(TextUtils.isEmpty(telNum)) {
				DialogUtils.alert(this, "提示", "请先输入电话号码？", null);
				return;
			}
			String url = Settings.IP + "/cgi-bin/phone_control.cgi?call=Z" + telNum;
			Utils.get(TELControlActivity.this, url, false);
			break;
		}
		input_phonenum.setText(num.toString());
	}
}

